﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;


using WindowSwitcher.Behaviors;

namespace WindowSwitcher
{
    /// <summary>
    /// Logique d'interaction pour HandleBindingUC.xaml
    /// </summary>
    public partial class ListboxCustomItem : UserControl, INotifyPropertyChanged
    {
        private BindingCollection lastSelectedItem;
        private KeyCollection lastSelectedKey;
        private ImageSource avatarSource;

        public event PropertyChangedEventHandler PropertyChanged;

        public ListboxCustomItem()
        {
            InitializeComponent();
            this.DataContext = this;

            //AvatarSource = new BitmapImage(new Uri("pack://application:,,,/WindowSwitcher;component/Images/unknown.png"));
        }
        
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //var view = CollectionViewSource.GetDefaultView(this.CBX1.ItemsSource);
            //view.Filter = o => (o as BindingCollection).Enabled == true;
            //setImageSource(PSource);
        }   

        public ImageSource AvatarSource
        {
            get { return this.avatarSource; }
            set
            {
                if(this.avatarSource != value)
                {
                    this.avatarSource = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public BindingsCollections CbxItemsSource1
        {
            get { return (BindingsCollections)GetValue(ItemSourceCBX); }
            set 
            { 
                SetValue(ItemSourceCBX, value);

                //var view = CollectionViewSource.GetDefaultView(this.CBX1.ItemsSource);
                //view.Filter = o => (o as BindingCollection).Enabled == true;
            }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemSourceCBX =
            DependencyProperty.Register("CbxItemsSource1", typeof(BindingsCollections), typeof(ListboxCustomItem));

        public KeysCollections CbxItemsSource2
        {
            get { return (KeysCollections)GetValue(ItemSourceCBX2); }
            set { SetValue(ItemSourceCBX2, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemSourceCBX2 =
            DependencyProperty.Register("CbxItemsSource2", typeof(KeysCollections), typeof(ListboxCustomItem));

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count != 0)
            {
                var item = e.AddedItems[0] as BindingCollection;
                CbxItemsSource1.FirstOrDefault(x => x == item).Enabled = false;
                CbxItemsSource1.FirstOrDefault(x => x == item).IsAttached = true;
                //App.Mediator.NotifyColleagues<BindingCollection>("SetWindowHandleByName", item);

                if (lastSelectedItem != null)
                {
                    lastSelectedItem.Key = System.Windows.Input.Key.None;
                    CbxItemsSource1.FirstOrDefault(x => x == lastSelectedItem).Enabled = true;
                    CbxItemsSource1.FirstOrDefault(x => x == lastSelectedItem).IsAttached = false;
                }

                lastSelectedItem = item;
            }
        }

        private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count != 0)
            {
                var item = e.AddedItems[0] as KeyCollection;
                CbxItemsSource2.FirstOrDefault(x => x == item).Enabled = false;
                CbxItemsSource2.FirstOrDefault(x => x == item).IsAttached = true;
                CbxItemsSource1.FirstOrDefault(x => x.HandleName == (CBX1.SelectedValue as BindingCollection).HandleName).Key = item.Key;                

                if (lastSelectedKey != null)
                {
                    CbxItemsSource2.FirstOrDefault(x => x == lastSelectedKey).Enabled = true;
                    CbxItemsSource2.FirstOrDefault(x => x == lastSelectedKey).IsAttached = false;
                }

                lastSelectedKey = item;
            }
        }

        public void setImageSource(string uri)
        {
            BitmapImage image = new BitmapImage();
            image.BeginInit();
            image.UriSource = new Uri("Images/" + uri as string, UriKind.Relative);
            image.EndInit();

            this.AvatarSource = image;

            this.Avatar = new Image() { Source = image };
        }

        private void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
